package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.util.JDBC;
import com.cn.wanxi.util.Result;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsImpl  implements NewsDao {
    /**
     * type=1标示在首页
     * 其他标示都可以显示
     * @return
     */
    @Override
    public List<NewsModel> getNewsModelListForHomePage() {
        String sql="select * from news where type=1 order by sort desc";
        List<NewsModel> list=new ArrayList<>();
        ResultSet resultSet= JDBC.query(sql,new ArrayList());
        try {
            while (resultSet.next()){
                NewsModel model=new NewsModel();
                model.setContent(resultSet.getString("content"));
                model.setTitle(resultSet.getString("title"));
                model.setId(resultSet.getInt("id"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<NewsModel> findByIdForFront(NewsModel newsModel) {
        String sql="select * from news where id=(select max(id) from news where id<? )\n" +
                "union all\n" +
                "select * from news where id=?\n" +
                "union all\n" +
                "select * from news where id=(select min(id) from news where id>? )";
        List<NewsModel> list=new ArrayList<>();
        List sqlList=new ArrayList();
        sqlList.add(newsModel.getId());
        sqlList.add(newsModel.getId());
        sqlList.add(newsModel.getId());
        ResultSet resultSet= JDBC.query(sql,sqlList);
        try {
            while (resultSet.next()){
                NewsModel model=new NewsModel();
                model.setContent(resultSet.getString("content"));
                model.setTitle(resultSet.getString("title"));
                model.setId(resultSet.getInt("id"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int add(NewsModel newsModel) {
        return 0;
    }

    @Override
    public int update(NewsModel newsModel) {
        return 0;
    }

    @Override
    public int delete(NewsModel newsModel) {
        return 0;
    }

    @Override
    public List<NewsModel> findAll(NewsModel newsModel) {
        return null;
    }

    @Override
    public NewsModel findById(NewsModel newsModel) {
        String sql="select * from news where id=?";
        List list=new ArrayList();
        list.add(newsModel.getId());
        ResultSet resultSet=JDBC.query(sql,list);
        NewsModel model=new NewsModel();
        try {
            while (resultSet.next()){
                model.setContent(resultSet.getString("content"));
                model.setTitle(resultSet.getString("title"));
                model.setId(resultSet.getInt("id"));
                model.setCreatTime(resultSet.getString("create_time"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int getCount(NewsModel newsModel) {
        return 0;
    }

    @Override
    public int changeEnable(NewsModel newsModel) {
        return 0;
    }
}
